package com.woniu.his.config;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/*
全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ModelAndView myerroe(Exception e) {
        ModelAndView mv = new ModelAndView();
        if (e instanceof NoHandlerFoundException) {
            mv.setViewName("404");
        } else if (e instanceof UnauthorizedException) {
            mv.setViewName("shiro-erro");
        } else {
            mv.setViewName("error-global");
        }
        mv.addObject("error", e.getMessage());
        return mv;
    }

    /***
     * 404处理
     * @return
     */
//    @ExceptionHandler(NoHandlerFoundException.class)
//    @ResponseStatus(HttpStatus.NOT_FOUND)
//    public String notFountHandler() {
//        return "404";
//    }

//    @ExceptionHandler()
//    public ModelAndView processUnauthenticatedException(HttpServletRequest request, UnauthorizedException e) {
//        System.out.println("没有权限异常");
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("exception", e);
//        mv.setViewName("shiro-erro");
//        return mv;
//    }

}

